package com.si.sea_island.utils;


import com.si.sea_island.base.JsonResult;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class CheckToken {
    public JsonResult checkJwtToken(String accessToken,String refreshToken) {
        //accessToken是否过期
        Claims tokenClaims = null;
        boolean isAccessExpired = false;
        Map<String, String> resultMap = new HashMap<>();
        try {
            tokenClaims = JwtTokenUtil.getClaims(accessToken);
        } catch (ExpiredJwtException e) {
            isAccessExpired = true;
            tokenClaims = e.getClaims();
        } catch (Exception e) {
//            return JsonResult.error(-1,"accessToken无效签名");
            return JsonResult.error(-1,"请重新登录");
        }
        String userId = String.valueOf(tokenClaims.get("userId"));
        String authType = String.valueOf(tokenClaims.get("authType"));
        //封装数据
        resultMap.put("userId", userId);
        resultMap.put("authType", authType);
        if (isAccessExpired) {
            //accessToken过期,查看refreshToken
            if (ObjectUtils.isEmpty(refreshToken)) {
//                return JsonResult.error(-1,"缺少refreshToken");
                return JsonResult.error(-1,"请重新登录");
            }
            Claims refreshTokenClaims = null;
            try {
                refreshTokenClaims = JwtTokenUtil.getClaims(refreshToken);
                //accessToken与refreshToken的数据进行校验
                String refreshUserId = String.valueOf(refreshTokenClaims.get("userId"));
                String refreshAuthType = String.valueOf(refreshTokenClaims.get("authType"));
//            log.info("校验token -> body = {}"+new Gson().toJson(tokenClaims));
//            log.info("校验token -> body = {}"+new Gson().toJson(refreshTokenClaims));
                if (userId.equals(refreshUserId)
                        && authType.equals(refreshAuthType)) {
                    if (isAccessExpired) {
                        log.info("accessToken过期处理,refreshToken有效，生成新的accessToken和refreshToken");
                        //accessToken过期处理,refreshToken有效，生成新的accessToken和refreshToken
                        String newJwtToken = JwtTokenUtil.createJwtToken(userId, authType);
                        String newJwtRefreshToken = JwtTokenUtil.createJwtRefreshToken(userId, authType);
                        resultMap.put("accessToken", newJwtToken);
                        resultMap.put("refreshToken", newJwtRefreshToken);
                    }
                } else {
//                    return JsonResult.error(-1,"accessToken与refreshToken校验错误");
                    return JsonResult.error(-1,"请重新登录");
                }
            } catch (ExpiredJwtException e) {
                return JsonResult.error(-1,"请重新登录");
            } catch (Exception e) {
//                return JsonResult.error(-1,"refreshToken无效签名");
                return JsonResult.error(-1,"请重新登录");
            }
        } else {

        }
        return JsonResult.success("验证成功").data(resultMap);
    }
}
